<template>
    <el-form :inline="true" class="demo-form-inline" :disabled="disabled">
        <el-form-item label="一级分类列表">
            <el-select v-model="categoryId1" placeholder="请选择" @change="handleCategoryChange1">
                <el-option :label="item.name" :value="item.id" v-for="item in categoryList1" :key="item.id" />
            </el-select>
        </el-form-item>
        <el-form-item label="二级分类列表">
            <el-select v-model="categoryId2" placeholder="请选择" @change="handleCategoryChange2">
                <el-option :label="item.name" :value="item.id" v-for="item in categoryList2" :key="item.id" />
            </el-select>
        </el-form-item>
        <el-form-item label="三级分类列表">
            <el-select v-model="categoryId3" placeholder="请选择">
                <el-option :label="item.name" :value="item.id" v-for="item in categoryList3" :key="item.id" />
            </el-select>
        </el-form-item>
    </el-form>
</template>

<script lang="ts" setup>
import { defineProps, onBeforeUnmount, onMounted } from "vue";
import { useCategoryStore } from "@/stores/category";
import { useSpuStore } from "@/stores/spu";
import { storeToRefs } from "pinia";

defineProps<{
    disabled?: boolean;
}>();
const categoryStore = useCategoryStore(); // 获取分类数据
const spuStore = useSpuStore(); // 获取spu数据
// 挂载完成以后获取一级分类数据
onMounted(() => {
    categoryStore.getCategoryList1();
});
// 去pinia仓库中获取分类数据
const { categoryList1, categoryId1, categoryList2, categoryId2, categoryList3, categoryId3 } = storeToRefs(categoryStore);
// 绑定一级分类切换事件
const handleCategoryChange1 = (categoryId1: number) => {
    categoryStore.getCategoryList2(categoryId1);
};
// 绑定二级分类切换事件
const handleCategoryChange2 = (categoryId2: number) => {
    categoryStore.getCategoryList3(categoryId2);
};
// 离开页面的时候清空数据
onBeforeUnmount(() => {
    categoryStore.clearCategoryList();
    spuStore.clearSpuList();
});
</script>
